<?php

namespace App\Observers;

use App\Models\Order;
use App\Services\LogService;
use App\Services\NotificationService;
use Illuminate\Support\Facades\Log;

class OrderObserver
{
    protected LogService $logService;
    protected NotificationService $notificationService;

    public function __construct(LogService $logService, NotificationService $notificationService)
    {
        $this->logService = $logService;
        $this->notificationService = $notificationService;
    }

    /**
     * Handle the Order "created" event.
     */
    public function created(Order $order): void
    {
        $this->logService->logOrder('created', $order);
        
        Log::info('Order created', [
            'order_id' => $order->id,
            'order_number' => $order->order_number,
            'user_id' => $order->user_id,
            'total_amount' => $order->total_amount,
        ]);
    }

    /**
     * Handle the Order "updated" event.
     */
    public function updated(Order $order): void
    {
        // 检查状态是否发生变化
        if ($order->isDirty('status')) {
            $oldStatus = $order->getOriginal('status');
            $newStatus = $order->status;
            
            $this->logService->logOrder('status_changed', $order, [
                'old_status' => $oldStatus,
                'new_status' => $newStatus,
            ]);

            // 发送状态变更通知
            if ($order->user) {
                $this->notificationService->sendOrderStatusNotification(
                    $order->user,
                    $order,
                    $oldStatus,
                    $newStatus
                );
            }

            Log::info('Order status changed', [
                'order_id' => $order->id,
                'order_number' => $order->order_number,
                'old_status' => $oldStatus,
                'new_status' => $newStatus,
            ]);
        }

        // 检查支付状态是否发生变化
        if ($order->isDirty('payment_status')) {
            $oldPaymentStatus = $order->getOriginal('payment_status');
            $newPaymentStatus = $order->payment_status;
            
            $this->logService->logOrder('payment_status_changed', $order, [
                'old_payment_status' => $oldPaymentStatus,
                'new_payment_status' => $newPaymentStatus,
            ]);

            Log::info('Order payment status changed', [
                'order_id' => $order->id,
                'order_number' => $order->order_number,
                'old_payment_status' => $oldPaymentStatus,
                'new_payment_status' => $newPaymentStatus,
            ]);
        }

        $this->logService->logOrder('updated', $order);
    }

    /**
     * Handle the Order "deleted" event.
     */
    public function deleted(Order $order): void
    {
        $this->logService->logOrder('deleted', $order);
        
        Log::info('Order deleted', [
            'order_id' => $order->id,
            'order_number' => $order->order_number,
            'user_id' => $order->user_id,
        ]);
    }

    /**
     * Handle the Order "restored" event.
     */
    public function restored(Order $order): void
    {
        $this->logService->logOrder('restored', $order);
        
        Log::info('Order restored', [
            'order_id' => $order->id,
            'order_number' => $order->order_number,
            'user_id' => $order->user_id,
        ]);
    }

    /**
     * Handle the Order "force deleted" event.
     */
    public function forceDeleted(Order $order): void
    {
        $this->logService->logOrder('force_deleted', $order);
        
        Log::info('Order force deleted', [
            'order_id' => $order->id,
            'order_number' => $order->order_number,
            'user_id' => $order->user_id,
        ]);
    }
}
